﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.VisualBasic;
using System.Security.Cryptography;
using Microsoft.Win32;
/// <summary>
/// Summary description for Encrypt
/// </summary>
/// 
namespace RESTAURANT
{
    public class Encrypt
    {
        static public string Encrypte(string S)
        {
            string kq = "";
            byte[] plainByte = UnicodeEncoding.ASCII.GetBytes(S);
            byte[] result;
            SHA1 sha = new SHA1CryptoServiceProvider();
            result = sha.ComputeHash(plainByte);
            kq = BitConverter.ToUInt64(result, 0).ToString();
            return kq;
        }
        static public string EncrypteString(string s)
        {
            string kq = Encrypte(s);
            byte[] bytes = Encoding.ASCII.GetBytes(kq);
            kq = "";
            for (int i = 0; i < bytes.Length; i++)
                kq += bytes[i];
            kq = kq.Substring(0, kq.Length - 1);
            kq = Encrypte(kq);
            if (kq.Length > 16) 
                kq = kq.Substring(0, 16);
            return kq;
        }
        static public string ReadFile(string sDuongDan)
        {
            try
            {
                Stream read = File.OpenRead(sDuongDan);
                byte[] Buff = new byte[read.Length];
                read.Read(Buff, 0, (int)read.Length);
                int lenght;
                lenght = Buff[0];
                string sMahoa = "";
                for (int i = 0; i < lenght; i++)
                    sMahoa += Convert.ToChar(Buff[i * 59 + 230]);
                read.Close();
                return sMahoa;
            }
            catch (Exception)
            {
                return "";
            }
        }

        static private void writeFile(string ketqua)
        {
            byte[] buff = new byte[3000];
            int i = 0;
            int n = 3000;
            if (File.Exists("SecuritySystem.dll"))
                File.Delete("SecuritySystem.dll");
            FileStream file;
            file = File.Create("SecuritySystem.dll");
            for (i = 0; i < n; i++)
                buff[i] = (byte)(VBMath.Rnd() * 128);

            if (ketqua == null || ketqua == "")
                ketqua = "CONGTYTNHHTMDVCNKHOISANGVIET";
            for (i = 0; i < ketqua.Length; i++)
                buff[i * 59 + 230] = Convert.ToByte(ketqua[i]);
            buff[0] = (byte)ketqua.Length;
            file.Write(buff, 0, n);
            file.Close();
        }
        static public bool Regtry()
        {
            try
            {
                Stream f = File.OpenRead("SecuritySystem.dll");
                f.Close();
            }
            catch (Exception)
            {
                writeFile(null);
                return false;
            }
            string info = ComPuter.GetKey();
            info = EncrypteString(info);
            string mahoa = Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\Keyboard", "KeyboardHigh", "").ToString();
            if (mahoa != info)
                return false;
            string key = EncrypteString(info);
            key += EncrypteString("280845148181219850937834184");
            key = EncrypteString(key);
            string KeyFile = ReadFile("SecuritySystem.dll");
            if (key == KeyFile)
                return true;
            return false;
        }
    }
}
